﻿using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Text;

namespace USERS.CYT369.COM.DAL.DbContexts
{
    public partial class CYT369NewsContext : Microsoft.EntityFrameworkCore.DbContext
    {
        private USERS.CYT369.COM.UTILITY.Connection m_Connection;
        public CYT369NewsContext()
        {
            m_Connection = USERS.CYT369.COM.UTILITY.SystemSettingService.Instance.NewsDB;
        }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            if (m_Connection != null)
            {
                if (m_Connection.DBType == USERS.CYT369.COM.UTILITY.ConstStringClass.SQLSERVER2008 ||
                    m_Connection.DBType == USERS.CYT369.COM.UTILITY.ConstStringClass.SQLSERVER2005)
                {
                    optionsBuilder.UseSqlServer(m_Connection.ConnectionString).ReplaceService<Microsoft.EntityFrameworkCore.Query.IQueryTranslationPostprocessorFactory, Microsoft.EntityFrameworkCore.SqlServer.Query.Internal.SqlServer2008QueryTranslationPostprocessorFactory>();
                }
                else
                {
                    optionsBuilder.UseSqlServer(m_Connection.ConnectionString);
                }
            }
            base.OnConfiguring(optionsBuilder);
        }

        protected override void OnModelCreating(ModelBuilder builder)
        {
            string schema = null;
            if (m_Connection != null)
            {
                schema = m_Connection.DBSchema;
            }

            DocModelCreating(builder, schema);
        }

        private void DocModelCreating(ModelBuilder modelBuilder, string schema)
        {
            #region Documents
            modelBuilder.Entity<USERS.CYT369.COM.MODELS.Articles.UserDocCategory>(entity => {
                entity.ToTable("TB_UserDocCategory", schema);
                entity.HasOne(c => c.Parent).WithMany(c => c.SubTypes).HasPrincipalKey(c => c.Id).HasForeignKey(c => c.ParentID);
                entity.Ignore(c => c.id).Ignore(c => c.text).Ignore(c => c.children).Ignore(c => c.state);

            });

            modelBuilder.Entity<USERS.CYT369.COM.MODELS.Articles.Document>(entity => {
                entity.ToTable("TB_Document", schema).HasKey(c => c.Id);
                entity.HasQueryFilter(c=>c.Deleted == false);
            });

            modelBuilder.Entity<USERS.CYT369.COM.MODELS.Articles.DocumentExt>(entity =>
            {
                entity.ToTable("TB_DocumentExt", schema);
                entity.HasKey(c => c.DocID);
                entity.HasOne(c => c.Doc).WithOne(c => c.DocExt).HasForeignKey<USERS.CYT369.COM.MODELS.Articles.DocumentExt>(c => c.DocID);
            });

            modelBuilder.Entity<USERS.CYT369.COM.MODELS.Articles.DocAttachment>(entity =>
            {
                entity.ToTable("TB_DocAttachment", schema);
                entity.HasOne(c => c.Doc).WithMany(c => c.Attachments).HasForeignKey(c => c.DocID);
            });

            #endregion
        }


    }
}
